"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A4检测项确保配置用户的有效期限检测与修复成功
Create At   : 2024/10/25
Owner       : lihongji
Description :
    1.创建用户test_se后检测A4项
    2.修复A4不符合安全规范项目后再次检测
    3.检查是否存在未配置有效期的用户
Expect      :
    1.创建用户成功，打印告警信息
    2.修复成功，不打印告警信息
    3.不存在未配置有效期的用户
History     :
"""
import os
import time
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):

        step = ('----step1:创建用户test_se后检测A4项 '
                'expect:创建用户成功，打印告警信息----')
        self.log.info(step)
        check_sql = ("drop user if exists test_se;"
                     "create user test_se identified by 'qwer*963.';")
        sql_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(sql_res)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res,
                      '执行失败' + step)
        self.assertIn("Ensure configuration of user expiration "
                      "periods", check_res, '执行失败' + step)

        step = ('----step2:修复A4不符合安全规范项目后再次检测 '
                'expect:不打印告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B4 --detail;'
                          f'gs_checkse -i A4 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Setting Account Password Management succeed",
                      check_res, '执行失败' + step)
        self.assertNotIn("Ensure correct configuration of account "
                         "automatic unlock time.", check_res, '执行失败' + step)

        step = ('----step3:检查是否存在未配置有效期的用户  '
                'expect:不存在未配置有效期的用户----')
        self.log.info(step)
        check_sql = """SELECT rolname, rolvalidbegin, rolvaliduntil 
                        FROM pg_roles 
                        WHERE rolsuper=false
                        AND rolname NOT LIKE 'gs_role%' 
                        AND (rolvalidbegin IS NULL OR rolvaliduntil IS NULL);
                        """
        sql_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(sql_res)
        self.assertIn('(0 rows)', sql_res, '执行失败' + step)

    def tearDown(self):
        step = '----step4:清理环境 expect:成功----'
        self.log.info(step)
        del_sql = "drop user if exists test_se;"
        del_res = self.sh_primary.execut_db_sql(del_sql)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, del_res,
                      '执行失败' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')